<?php
/**
 * LogAnalysis Job
 *
 * @author tamhv@fpt.net
 */

class JobLogError
{
    /**
     * Job send error message
     *
     */
    public function sendErrorMessage($data)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //include config send mail
            include(APPLICATION_PATH.'/configs/mail.php');
            //check receiver log
            if(is_array($logReceivers) && !empty($logReceivers))
            {
                //init error type
                $arrType = array(
                   '1' => 'Action',
                   '2' => 'Client side',
                   '3' => 'API',
                   '4' => 'Model'
                );
                //default request
                $request = '';
                
                if(!empty($data['request_info']))
                {
                    foreach ($data['request_info'] as $key => $value)
                    {
                        $request.=$key .' => ' .$value.'<br/>';
                    }
                }                
                //init content email log
                $fromtype   = 'message';
                $date       = date('d/m/Y H:i:s',$data['time']);
                $body       = 'Type: '.$arrType[$data['type']].'<br/>';
                $body       .= 'Time: '.$date.'<br/>';
                if(!empty($data['request_url']))
                {
                    $body .= 'Request Url: ' . $data['request_url'] . '<br/>';
                }
                if(!empty($request))
                {
                    $body .= 'Request Info: <br/>' . $request . '<br/>';
                }
                $body .= 'Error Code: '.$data['error_code'].'<br/>';
                $body .= 'Message: <br/>'.$data['error_message'].'<br/><br/>';
                $body .= 'Stack trace: <br/>'.nl2br($data['stack_trace']);
                //end init  content email log
                //
                //Create transport
                $transport = new Zend_Mail_Transport_Smtp(isset($smtpServer[$fromtype])?$smtpServer[$fromtype]:$smtpServer['default'], $configmail);
                //init zend mail
                $mail = new Zend_Mail('UTF-8');
                //set from
                $mail->setFrom($configsender[$fromtype]['email'],$configsender[$fromtype]['name']);
                //foreach receivers
                foreach($logReceivers as $recipient)
                {
                    $mail->addTo($recipient['email'], $recipient['name']);
                }              
                //set content mail
                $mail->setSubject($data['subject'] .' - '.$date.' - '.$data['error_message']);
                $mail->setBodyHtml($body);
                //send mail
                if(!$mail->send($transport))
                {
                    $response['error'] = 1;
                    $response['message'] .= 'Mail err: can not send to: '.$email;
                }
            }
            else
            {
                $response['error'] = 1;
                $response['message'] .= 'Empty recipient';
            }
        }
        catch (Exception $ex)
        {
            $response['error'] = 1;
            $response['message'] = $ex->getMessage();
        }

        // Return response
        return $response;
        
    }
}
?>
